package fr.teuteuf.runstickmanrun.entity;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;

import fr.teuteuf.runstickmanrun.Animation;
import fr.teuteuf.runstickmanrun.Constants;

public class BigLeftMonster extends AbstractEntity {
	
	public static final int INDEX_SLEEPING_ANIMATION = 0;
	public static final int INDEX_RUNNING_ANIMATION  = 1;
	
	private static final int NB_ANIMATIONS = 2;
	private static final int FRAME_WIDTH  = 640;
	private static final int FRAME_HEIGHT = 640;
	
	private static final int SPRITE_WIDTH  = 120;
	private static final int SPRITE_HEIGHT = 640;
	
	private static Texture texture;
	
	private final float SLEEPING_DURATION = 5f;
	private float sleepingTime = 0f;
	private float speedLimit;
	
	public BigLeftMonster(World world, int x, int y, float movingSpeed) {
		jumpForce = 0f;
		if(Constants.IS_DEBUG_MODE) {
			this.movingSpeed = 0;
		} else {
			this.movingSpeed = movingSpeed * .60f;
		}
		this.speedLimit  = movingSpeed * .75f;
		
		initAnimations();
		currentAnimation = animations[INDEX_SLEEPING_ANIMATION];
		
		initBody(world, x, y);
	}

	@Override
	protected void initAnimations() {
		animations = new Animation[NB_ANIMATIONS];
		
		Animation tempAnimation;
		
		//Sleeping animation
		tempAnimation = new Animation();
		tempAnimation.addFrame(texture, 2 * FRAME_WIDTH, 2 * FRAME_HEIGHT, FRAME_WIDTH, FRAME_HEIGHT, 1);
		animations[INDEX_SLEEPING_ANIMATION] = tempAnimation;
		
		//Running animation
		tempAnimation = new Animation();
		tempAnimation.addFrame(texture, 0 * FRAME_WIDTH, 0 * FRAME_HEIGHT, FRAME_WIDTH, FRAME_HEIGHT, .1f);
		tempAnimation.addFrame(texture, 1 * FRAME_WIDTH, 0 * FRAME_HEIGHT, FRAME_WIDTH, FRAME_HEIGHT, .1f);
		tempAnimation.addFrame(texture, 2 * FRAME_WIDTH, 0 * FRAME_HEIGHT, FRAME_WIDTH, FRAME_HEIGHT, .1f);
		tempAnimation.addFrame(texture, 0 * FRAME_WIDTH, 1 * FRAME_HEIGHT, FRAME_WIDTH, FRAME_HEIGHT, .1f);
		tempAnimation.addFrame(texture, 1 * FRAME_WIDTH, 1 * FRAME_HEIGHT, FRAME_WIDTH, FRAME_HEIGHT, .1f);
		tempAnimation.addFrame(texture, 2 * FRAME_WIDTH, 1 * FRAME_HEIGHT, FRAME_WIDTH, FRAME_HEIGHT, .1f);
		tempAnimation.addFrame(texture, 0 * FRAME_WIDTH, 2 * FRAME_HEIGHT, FRAME_WIDTH, FRAME_HEIGHT, .1f);
		tempAnimation.addFrame(texture, 1 * FRAME_WIDTH, 2 * FRAME_HEIGHT, FRAME_WIDTH, FRAME_HEIGHT, .1f);
		animations[INDEX_RUNNING_ANIMATION] = tempAnimation;
	}

	@Override
	protected void initBody(World world, int x, int y) {
		BodyDef bodyDef = new BodyDef();
		bodyDef.type = BodyType.KinematicBody;
		bodyDef.position.set(new Vector2((x + SPRITE_WIDTH/2) * Constants.WORLD_TO_BOX, (y + SPRITE_HEIGHT/2) * Constants.WORLD_TO_BOX));
		body = world.createBody(bodyDef);
		
		PolygonShape shape = new PolygonShape();
		shape.setAsBox(SPRITE_WIDTH/2 * Constants.WORLD_TO_BOX, SPRITE_HEIGHT/2 * Constants.WORLD_TO_BOX);
		
		FixtureDef fixtureDef = new FixtureDef();
		fixtureDef.shape = shape;
		fixtureDef.isSensor = true;
		
		body.createFixture(fixtureDef);
		body.setUserData(this);
		shape.dispose();
		
		
	}

	@Override
	public void updateCurrentAnimation() {
		if(velocity.x > 0) {
			currentAnimation = animations[INDEX_RUNNING_ANIMATION];
		}
	}
	
	@Override
	public void update(float elapsedTime) {
		if(movingSpeed < speedLimit) {
			movingSpeed += movingSpeed * .01f * elapsedTime;
		}
		
		super.update(elapsedTime);
		
		if(sleepingTime < SLEEPING_DURATION) {
			moveRight(false);
			sleepingTime += elapsedTime;
		} else {
			moveRight(true);
		}
	}
	
	public static void loadTexture() {
		texture = new Texture(Gdx.files.internal("sprites/platformer_sprites_base_giant.png"));
	}
	
	public static void dispose() {
		texture.dispose();
	}
}
